package cn.tedu._03vehicle.base.interceptors;

import cn.tedu._03vehicle.pojo.vo.UserVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 权限校验:登录状态校验的拦截器;
 */
@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("进入登录状态校验的拦截器...");
        /*
            1.获取会话HttpSession;
              HttpSession session = request.getSession();
            2.从HttpSession中获取当前用户信息;
            3.判断用户信息是否为空;
              3.1 为空: 未登录, 重定向到登录页面/login.html, return false;
                  response.sendRedirect("/login.html");
              3.2 不为空: 已登录, 放行, return true;
         */
        HttpSession session = request.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){//未登录
            response.sendRedirect("/login.html");
            return false;
        }
        //放行
        return true;
    }
}








